/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package my12306;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;
import my12306.http.Consts;
import my12306.table.KeyValueItem;
import org.jdesktop.application.Application;
import org.jdesktop.application.ResourceMap;

/**
 * 此类用于显示车票查询结果。点列名进行排序。 支持多选。 JTable用法的更多信息请参考：
 * http://docs.oracle.com/javase/tutorial/uiswing/components/table.html
 *
 * @author Jack
 */
public class TrainInfoPanel extends javax.swing.JPanel {

    TrainInfoModel model;
    ResourceMap resourceMap = Application.getInstance(my12306.My12306App.class).getContext().getResourceMap(TrainInfoPanel.class);

    /**
     * Creates new form TrainInfoPanel
     */
    public TrainInfoPanel() {
        this(new ArrayList<List<Object>>(), Consts.columns);
    }

    public TrainInfoPanel(List<List<Object>> data, String[] columns) {
        super(new GridLayout(1, 0));
        initComponents();

        model = new TrainInfoModel(data, columns);
        table.setModel(model);
        table.setFillsViewportHeight(true);
        table.setAutoCreateRowSorter(true);
        table.setPreferredScrollableViewportSize(new Dimension(800, 360));
        table.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        init();
    }

    private void init() {
        //from station
        TableColumn startStation = getTable().getColumnModel().getColumn(1);

        startStation.setCellRenderer(new DefaultTableCellRenderer() {
            @Override
            public Component getTableCellRendererComponent(JTable table, Object value,
                    boolean isSelected, boolean hasFocus, int row, int column) {
                Object result = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

                if (value instanceof KeyValueItem && result instanceof JLabel) {
                    KeyValueItem entry = (KeyValueItem) value;
                    JLabel label = (JLabel) result;
                    label.setText(entry.getKey());
                    label.setIcon(resourceMap.getIcon(entry.getValue()));
                    return label;
                } else {
                    return (Component) result;
                }
            }
        });

        //to station
        TableColumn toStation = getTable().getColumnModel().getColumn(2);

        toStation.setCellRenderer(new DefaultTableCellRenderer() {
            @Override
            public Component getTableCellRendererComponent(JTable table, Object value,
                    boolean isSelected, boolean hasFocus, int row, int column) {
                Object result = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

                if (value instanceof KeyValueItem && result instanceof JLabel) {
                    KeyValueItem entry = (KeyValueItem) value;
                    JLabel label = (JLabel) result;
                    label.setText(entry.getKey());
                    label.setIcon(resourceMap.getIcon(entry.getValue()));
                    return label;
                } else {
                    return (Component) result;
                }
            }
        });
    }

    public Object getValueAt(int row, int col) {
        return model.getValueAt(row, col);
    }

    /**
     * @param rowData the rowData to set
     */
    public void setRowData(List<List<Object>> rowData) {
        this.model.setRowData(rowData);
    }

    public List<Object> getSelectedRowData() {
        int viewRow = getTable().getSelectedRow();
        viewRow = getTable().convertRowIndexToModel(viewRow);
        return model.getRowData(viewRow);
    }

    public List<List<Object>> getSelectedRows() {
        List<List<Object>> result = new ArrayList<List<Object>>();
        int[] rows = getTable().getSelectedRows();
        for (int i : rows) {
            int viewRow = getTable().convertRowIndexToModel(i);
            result.add(model.getRowData(viewRow));
        }
        return result;
    }

    /**
     * @param columnNames the columnNames to set
     */
    public void setColumnNames(String[] columnNames) {
        this.model.setColumnNames(columnNames);
    }

    /**
     * @return the table
     */
    public javax.swing.JTable getTable() {
        return table;
    }

    class TrainInfoModel extends AbstractTableModel {

        private List<List<Object>> rowData;
        private String[] columnNames;

        public TrainInfoModel() {
        }

        public TrainInfoModel(List<List<Object>> data, String[] columns) {
            this.rowData = data;
            this.columnNames = columns;
        }

        public int getColumnCount() {
            return getColumnNames().length;
        }

        public int getRowCount() {
            return getRowData().size();
        }

        @Override
        public String getColumnName(int col) {
            return getColumnNames()[col];
        }

        public Object getValueAt(int row, int col) {
            if (row >= 0 && row < rowData.size()) {
                return rowData.get(row).get(col);
            } else {
                return "";
            }
        }

        @Override
        public Class getColumnClass(int col) {
            return getValueAt(0, col).getClass();
//            return String.class;
        }

        @Override
        public boolean isCellEditable(int row, int col) {
            return false;
        }

        /**
         * @return the rowData
         */
        public List<List<Object>> getRowData() {
            return rowData;
        }

        public List<Object> getRowData(int row) {
            return rowData.get(row);
        }

        /**
         * @param rowData the rowData to set
         */
        public void setRowData(List<List<Object>> rowData) {
            this.rowData = rowData;
            fireTableDataChanged();
            updateUI();
        }

        /**
         * @return the columnNames
         */
        public String[] getColumnNames() {
            return columnNames;
        }

        /**
         * @param columnNames the columnNames to set
         */
        public void setColumnNames(String[] columnNames) {
            this.columnNames = columnNames;
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        table = new javax.swing.JTable();

        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        table.setFillsViewportHeight(true);
        table.setName("table"); // NOI18N
        jScrollPane1.setViewportView(table);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 584, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 332, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JScrollPane jScrollPane1;
    public javax.swing.JTable table;
    // End of variables declaration//GEN-END:variables
}
